﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

namespace IAATO.Admin
{
    public partial class ActiviteSiteLinkManagement : System.Web.UI.Page
    {
        protected ActiviteSiteLinkService activiteSiteLinkService;
        protected SiteService siteService;
        protected ActiviteService activiteService;

        protected void Page_Load(object sender, EventArgs e)
        {
            siteService = new SiteService(new SiteManager(), new ActiviteManager(), new CreneauService(new CreneauManager()));
            activiteService = new ActiviteService(new ActiviteManager(), new SiteManager());
            activiteSiteLinkService = new ActiviteSiteLinkService(new ActiviteSiteLinkManager());

            #region binding gridView

            List<ACTIVITE_SITE_LINK> links = activiteSiteLinkService.getAllActiviteSiteLinks();
            
            if (links == null)
            {
                lblError.Text = "An error has occurred";
            }
            else if (links.Count == 0)
            {
                lblError.Text = "No place possibility in the database";
                gdwLinks.DataBind();
            }
            else
            {
                lblError.Text = "";
                gdwLinks.DataSource = links;
                gdwLinks.DataBind();
            }


            if (!IsPostBack)
            {
                ddlSite.DataSource = siteService.getAllSites();
                ddlSite.DataTextField = "NOM";
                ddlSite.DataValueField = "ID";
                ddlSite.DataBind();

                ddlActivite.DataSource = activiteService.getAllActivites();
                ddlActivite.DataTextField = "NOM";
                ddlActivite.DataValueField = "ID";
                ddlActivite.DataBind();
            }


            #endregion
        }

        protected void btnAjouter_Click(object sender, EventArgs e)
        {
            activiteSiteLinkService.createActiviteSiteLink(int.Parse(ddlActivite.SelectedValue), int.Parse(ddlSite.SelectedValue));
            Response.Redirect("ActiviteSiteLinkManagement.aspx");
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            string strIds = ((ImageButton)sender).CommandArgument;
            string[] tabIds = strIds.Split(';');

            activiteSiteLinkService.deleteActiviteSiteLink(int.Parse(tabIds[0]), int.Parse(tabIds[1]));

            Response.Redirect("ActiviteSiteLinkManagement.aspx");
        }

        protected void gdwLinks_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdwLinks.DataSource = activiteSiteLinkService.getAllActiviteSiteLinks();
            gdwLinks.PageIndex = e.NewPageIndex;
            gdwLinks.DataBind(); 
        }

    }
}